package performace_test;

import java.util.Random;

import heap.CandyMaxHeap;

/**
 * 
 * @Title: HeapPerformaceTest.java 
 * @Package performace_test 
 * @Description: 将数组依次加入一个空堆中构建一个最大堆和基于数组直接构建最大堆性能对比
 * @author CandyWall   
 * @date 2021年1月8日 下午11:35:37 
 * @version V1.0
 */
public class HeapPerformaceTest {
    private static double testHeap(Integer[] testData, boolean isHeapify) {
        long start = System.nanoTime();
        CandyMaxHeap<Integer> maxHeap = null;
        if(isHeapify) {
            maxHeap = new CandyMaxHeap<>(testData);
        } else {
            maxHeap = new CandyMaxHeap<>();
            for(Integer num : testData) {
                maxHeap.add(num);
            }
        }
        return (System.nanoTime() - start) / 1000_000_000.0;
    }
    
    public static void main(String[] args) {
        int count = 1000_0000;
        Random random = new Random();
        // 生成随机测试数据
        Integer[] testData = new Integer[count];
        for(int i = 0; i < count; i++) {
            testData[i] = random.nextInt(Integer.MAX_VALUE);
        }
        System.out.println("将数组元素依次加入空堆中构建最大堆耗时：" + testHeap(testData, false) + " s");
        System.out.println("直接基于数组构建最大堆耗时：" + testHeap(testData, true) + " s");
    }
}
